--[[
Copyright (c) 2011 Thomas Bentz, DigitalBlur Interactive Inc.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--]]

-- download the xml data
-- params: url - url to use for downloading the xml data
xml = {};
function xml:getXMLData(url)
	local srcfile = "temp.xml"
	local path = system.pathForFile(srcfile, system.DocumentsDirectory)
	local myFile = io.open( path, "w" )
	local xmltext = http.request{
		url = url, 
	    sink = ltn12.sink.file(myFile),
	}
	return XmlParser:ParseXmlFile(path)
end
-- run the query
function xml:doQuery(url)
	-- do it
	print(url)
	local xmlData = xml:getXMLData(url)
	return xmlData
end
-- parse xml data and display forecast
-- params: xmlTree - table of xml node data
--[[function displayData(xmlTree)
	local simpleforecast = xmlNodes(xmlTree, "simpleforecast")
	native.setActivityIndicator( true )
	for i,xmlNode in pairs(simpleforecast) do
		if(xmlNode.Name == "forecastday") then
			local forecast = {}
			forecast.period = xmlValue(xmlNode, "period")
			forecast.high = xmlNodes(xmlNode, "high")[1].Value
			forecast.low = xmlNodes(xmlNode, "low")[1].Value
			forecast.condition = xmlValue(xmlNode, "conditions")
			-- date info
			local dateNodes = xmlNodes(xmlNode, "date")
			forecast.day = dateNodes[4].Value
			forecast.month = dateNodes[5].Value
			forecast.year = dateNodes[6].Value
			forecast.monthname = dateNodes[12].Value
			forecast.weekday = dateNodes[14].Value
			forecast.ampm = dateNodes[15].Value
			-- icons
			local icons = xmlNodes(xmlNode, "icons")
			forecast.link = icons[8].ChildNodes[1].Value
			Display:getImage(forecast)
			-- display forecast enclosure
			local displayForecastClosure = function () return Display:displayForecast(forecast) end
			timer.performWithDelay(2000, displayForecastClosure)
		end
	end
	native.setActivityIndicator( false ) -- show busy
end--]]
-- xmlValue function returns an xml value
-- params: xmlTree - table of xml node data
-- params: nodeName - name of node to look for
-- return: string value
function xml:xmlValue(xmlTree, nodeName)
	for i,xmlNode in pairs(xmlTree.ChildNodes) do
		--print(xmlNode.Name)
		if(xmlNode.Name==nodeName) then
			return xmlNode.Value
		end
	end
end

-- xmlNodes function returns xml node(s)
-- params: xmlTree - table of xml node data
-- params: nodeName - name of node to look for
-- return: table of children nodes
function xml:xmlNodes(xmlTree, nodeName)
	for i,xmlNode in pairs(xmlTree.ChildNodes) do
		--print(xmlNode.Name)
		if(xmlNode.Name==nodeName) then
			return xmlNode.ChildNodes
		end
	end
end